package at.fhooe.ems.dal;


import java.sql.*;
import javax.swing.JOptionPane;

public class DBConnector {

    static Connection con;
    static ResultSet rs;
    static int count;
    

    public static void connectDb() {
       try {
            Class.forName("com.mysql.jdbc.Driver");
            // con=DriverManager.getConnection("jdbc:mysql://localhost"+":3300/contact","root","bitplanet");
             con=DriverManager.getConnection("jdbc:mysql://ec2-50-19-213-178.compute-1.amazonaws.com"+":3306/contact","bitplanet","bitplanet");
           }
       
        catch (Exception ex) {
            ex.printStackTrace();
            //JOptionPane.showMessageDialog(null, ex, "Error",JOptionPane.ERROR_MESSAGE);
            //JOptionPane.showMessageDialog(null, "Sorry!!! Unable to connect", "Operations cannot be performed", JOptionPane.ERROR_MESSAGE);
        }
    }
   

    public static ResultSet processQuery(String strSQL) {
        try {
            /*Create a Statement os the SELECT statement*/
            Statement stmt = con.createStatement();
            /*Send the data request to a database and process the SELECT
            statement*/
            rs = stmt.executeQuery(strSQL);
            return rs;
        } catch (Exception ex) {
            ex.printStackTrace();
           // JOptionPane.showMessageDialog(null, "No Match/Record Found", "Void", JOptionPane.ERROR_MESSAGE);
           // DBConnector.connectDb();
            return null;

        }
    }
   

    public static int processDML(String strSQL) {
        try {
            /*Create a Statement object to process the INSERT/DELETE/UPDATE
            statement*/
            Statement stmt = con.createStatement();
            /*Send the data request to RDBMS and process the SELECT statement*/
            count = stmt.executeUpdate(strSQL);
            return count;
        } catch (Exception ex) {
            ex.printStackTrace();
         
            return 0;
        }
    }

    public static ResultSet processSQuery(String strSQL) {
        try {
            Statement stat = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
            rs = stat.executeQuery(strSQL);
            return rs;
        } catch (Exception e) {

            e.printStackTrace();
  
            return null;
        }

    }

    public static void closeDb() {
        try {
            con.close();
        } catch (Exception sq) {
            sq.printStackTrace();
        }

    }
}
